package cn.lbd.arithmetic.leetcode.editor.cn;
//计算给定二叉树的所有左叶子之和。
//
// 示例： 
//
// 
//    3
//   / \
//  9  20
//    /  \
//   15   7
//
//在这个二叉树中，有两个左叶子，分别是 9 和 15，所以返回 24 
//
// 
// Related Topics 树 
// 👍 202 👎 0


//leetcode submit region begin(Prohibit modification and deletion)


/*class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;

    TreeNode(int x) {
        val = x;
    }

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }

    public TreeNode getLeft() {
        return left;
    }

    public void setLeft(TreeNode left) {
        this.left = left;
    }

    public TreeNode getRight() {
        return right;
    }

    public void setRight(TreeNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "TreeNode{" +
                "val=" + val +
                '}';
    }
}

class Solution404 {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        TreeNode heroTreeNode3 = new TreeNode(3);
        TreeNode heroTreeNode5 = new TreeNode(5);
        TreeNode heroTreeNode7 = new TreeNode(7);
        TreeNode heroTreeNode9 = new TreeNode(9);
        TreeNode heroTreeNode10 = new TreeNode(10);
        TreeNode heroTreeNode11 = new TreeNode(11);

        root.setLeft(heroTreeNode3);
        root.setRight(heroTreeNode5);
        heroTreeNode5.setLeft(heroTreeNode7);
        heroTreeNode5.setRight(heroTreeNode9);
        heroTreeNode9.setLeft(heroTreeNode10);
        heroTreeNode9.setRight(heroTreeNode11);

        System.out.println(sumOfLeftLeaves(root));


    }

    //如果树是空的就返回0，否则返回累加值
    public static int sumOfLeftLeaves(TreeNode root) {
        return (root == null) ? 0 : dfs(root);
    }

    *//**
     * 利用递归的会退性，完成对树的深搜
     * @param node
     * @return
     *//*
    public static int dfs(TreeNode node) {
        int ant = 0;
        if (node.left != null){
            //判断当前值的左孩子是不是叶子，是的话直接累加左孩子的值，否则继续向左递归
            ant += isLeafNode(node.left) ? node.left.val : dfs(node.left);
        }
        //左边走到底利用递归特性回退到根，再向右遍历，右边不为空且不是叶子，就递归的向右dfs
        // 他会将右孩子变成新的根，重新的从右孩子的左孩子开始筛选
        if (node.right != null && !isLeafNode(node.right)){
            ant += dfs(node.right);
        }
        return ant;
    }

    //判断当前节点是不是叶子
    public static boolean isLeafNode(TreeNode node) {
        return node.left == null && node.right == null;
    }




}*/
//leetcode submit region end(Prohibit modification and deletion)
